﻿using Autofac.Features.Indexed;
using CommunityToolkit.Mvvm.ComponentModel;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Unicode;
using System.Threading.Tasks;

namespace Models.Entities
{
    public class EquInfo : ObservableObject
    {
        public EquInfo() { }
        [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
        public int Id { get; set; }
        public string? SN { get; set; }
        public string Mac { get; set; }
        public string? Ip { get; set; }
        public string? GateWay { get; set; }
        public string? Mask { get; set; }
        public int? Vlan { get; set; }
        public int IpRole { get; set; } = 0;
        public string BootVersion { get; set; }
        public string ImageVersion { get; set; }
        public string VendorVersion { get; set; }
        public string KennelVersion { get; set; }
        public string HardwareVersion { get; set; }
        public string? ProductLine { get; set; }
        public string? TypeId { get; set; }
        public string? Oid { get; set; }
        public string? Description { get; set; }

        public void Init(byte[] reciveData)
        {
            int index = 0;
            while (index < reciveData.Length)
            {
                int dataLength = reciveData[index + 3];
                int dataType = reciveData[index + 1];
                dataLength += 4;
                int skipLength = dataLength;
                if (dataLength % 2 != 0)
                {
                    skipLength = dataLength + 1;
                }
                var body = reciveData[(index + 4)..(dataLength + index >= reciveData.Length - 1 ? reciveData.Length - 1 : dataLength + index)];
                SetValue(dataType, body);
                index += skipLength;
            }
        }
        private void SetValue(int type, byte[] data)
        {
            switch (type)
            {
                case 1:
                    BootVersion = Encoding.UTF8.GetString(data);
                    break;
                case 2:
                    KennelVersion = Encoding.UTF8.GetString(data);
                    break;
                case 3:
                    ImageVersion = Encoding.UTF8.GetString(data);
                    break;
                case 4:
                    HardwareVersion = Encoding.UTF8.GetString(data);
                    break;
                case 5:
                    VendorVersion = Encoding.UTF8.GetString(data);
                    break;
                case 6:
                    ProductLine = Encoding.UTF8.GetString(data);
                    break;
                case 7:
                    TypeId = Encoding.UTF8.GetString(data);
                    break;
                case 8:
                    Oid = Encoding.UTF8.GetString(data);
                    break;
                case 9:
                    SN = Encoding.UTF8.GetString(data);
                    break;
                case 10:
                    BootVersion = Encoding.UTF8.GetString(data);
                    break;
                //mac地址
                case 11:
                    Mac=BitConverter.ToString(data);
                    break;
                case 14:
                    Ip = $"{data[0]}.{data[1]}.{data[2]}.{data[3]}";
                    Mask = $"{data[4]}.{data[5]}.{data[6]}.{data[7]}";
                    GateWay = $"{data[8]}.{data[9]}.{data[10]}.{data[11]}";
                    Vlan = data[15];
                    //IpRole = $"{data[19]}";
                    break;
                default:
                    break;
            }
        }
    }
}
